package com.jxwifi.cloud.quickclean.mycenter;

import android.content.Context;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.idroid.widget.Toaster;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.AuzHttp;
import com.jxwifi.cloud.quickclean.okhttp.Params;

import cn.trinea.android.common.util.REPattern;

public class SetPhonePresenter {

    private Context mContext;
    private SetPhoneView mView;
    private AuzImpl mAuz;

    private CountDownTimer timer;

    public SetPhonePresenter(Context context,SetPhoneView view){
        this.mContext=context;
        this.mView=view;
        mAuz=new AuzImpl();
    }

    public void validation(){
        if ( mView.getPhone().getText().toString().equals("")) {
            Toaster.show(mContext, "请输入手机号码");
            return;
        }
        boolean isMobile = REPattern.isMobileNO( mView.getPhone().getText().toString());
        if (!isMobile) {
            Toaster.show(mContext, "请输入正确的手机号码");
            return;
        }
        setTimerCount();
        SendIdentCode();
    }

    public void setTimerCount(){
        timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mView.getObtainVerification().setText("验证码(" + millisUntilFinished / 1000 + "s)");
                mView.getObtainVerification().setEnabled(false);
            }

            @Override
            public void onFinish() {
                timer.cancel();
                mView.getObtainVerification().setEnabled(true);
                mView.getObtainVerification().setText("重获验证码");
            }
        };
        timer.start();
    }

    public void SendIdentCode(){
        Params params=new Params();
        params.add("mobile",mView.getPhone().getText().toString());
        params.add("type", " TELL");
        mAuz.auzget(UrlPool.POST_CAPTCHA_VALIDATE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {

            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode,msg);
            }
        });
    }

    public void judge(){
        if (mView.getPhone().getText().toString().equals("")) {
            Toaster.show(mContext, "请输入手机号码");
            return;
        }
        boolean isMobile = REPattern.isMobileNO(mView.getPhone().getText().toString());
        if (!isMobile) {
            Toaster.show(mContext, "请输入正确的手机号码");
            return;
        }
        if (mView.getPhoneVerification().getText().toString().equals("")) {
            Toaster.show(mContext, "验证码不能为空");
            return;
        }
        //作保存手机号码的操作
        if (!TextUtils.isEmpty(mView.getPhone().getText().toString())) {
            AccountMgr.putLastPhone(mContext, mView.getPhone().getText().toString());
        }
        logining();
    }

    public void logining(){
        Params params=new Params();
        params.add("code",mView.getPhoneVerification().getText().toString());
        params.add("tell",mView.getPhone().getText().toString());
        mAuz.auzget(UrlPool.UPDATE_TELL, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {

                if (code==0){
                    JSONObject obj = JSON.parseObject(data);
                    String token=obj.getString("token");
                    AccountMgr.puttoken(mContext,token);
                    AuzHttp.KJB_APP_Token=AccountMgr.gettoken(mContext);  //token添加到请求头
                    String userInfo=obj.getString("userInfo");
                    InfoBean mInfoBean=JSON.parseObject(userInfo,InfoBean.class);

                    Log.w("oushuhua","login Token="+ AuzHttp.KJB_APP_Token);
                    AccountMgr.putUser(mContext,mInfoBean,true);
                    AccountMgr.loginSuccess(mContext);
                    mView.onSuccess();
                }else{
                   mView.onlogining();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode,msg);
            }
        });
    }

}
